package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsDefBusiness;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DankMerchantsDefBusinessMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserBusinessRestService;
import com.syni.mdd.sjb.common.datatreaure.service.DankMerchantsDefBusinessService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-09-17
 */
@Service
public class DankMerchantsDefBusinessServiceImpl extends ServiceImpl<DankMerchantsDefBusinessMapper, DankMerchantsDefBusiness> implements DankMerchantsDefBusinessService {
    @Autowired
    DankMerchantsDefBusinessMapper dankMerchantsDefBusinessMapper;

    @Autowired
    UserBusinessRestService userBusinessRestService;

    @Override
    public boolean haveChooseDefaultBusiness(String merchantsAccountId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DankMerchantsDefBusiness.MERCHANTS_ACCOUNT_ID,merchantsAccountId);
        return CollectionUtils.isNotEmpty(dankMerchantsDefBusinessMapper.selectList(queryWrapper));
    }

    @Override
    public DankMerchantsDefBusiness getChooseBusiness(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DankMerchantsDefBusiness.USER_ID,userId);
        List<DankMerchantsDefBusiness> dankMerchantsDefBusinesses = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(dankMerchantsDefBusinesses)){
            return dankMerchantsDefBusinesses.get(0);
        }
        return null;
    }

    @Override
    public Integer getChooseBusinessId(Integer userId) {
        DankMerchantsDefBusiness dankMerchantsDefBusiness = getChooseBusiness(userId);
        if(dankMerchantsDefBusiness != null){
            return dankMerchantsDefBusiness.getBusinessId();
        }
        //判断是否是店员
        try {
            RpcResponseVo rpcResponseVo = userBusinessRestService.getUserBelongBusinessAssistant(userId);
            if(rpcResponseVo.isSuccess()){
                return Integer.parseInt(rpcResponseVo.getDataElement("bmsBusinessId").toString());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null;
    }
}
